package com.liit.front.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.liit.front.filter.WebContext;
import com.liit.module.dto.PathDto;
import com.liit.module.entity.Brand;
import com.liit.module.entity.Carousel;
import com.liit.module.entity.Product;
import com.liit.module.entity.ProductCategory;
import com.liit.module.frame.utils.ConfigUtil;
import com.liit.module.frame.utils.Const;
import com.liit.module.frame.utils.FileUtil;
import com.liit.module.frame.utils.page.Pagination;
import com.liit.module.frame.utils.spring.SpringUtils;
import com.liit.module.service.BrandService;
import com.liit.module.service.CarouselService;
import com.liit.module.service.FilesService;
import com.liit.module.service.ProductCategoryService;
import com.liit.module.service.ProductService;
import com.liit.module.service.TemplateService;

/**
 * @author dingweibo
 * @Title: IndexController.java
 * @Description: 首页
 * @date 2015年12月1日
 */
@Controller
public class IndexController {

	@Resource
	CarouselService carouselService;

	@Resource
	ProductService productService;

	@Resource
	FilesService filesService;

	@Resource
	TemplateService templateService;

	/**
	 * 首页
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/")
	public String index() {
		navigation();
		Pagination pagination = new Pagination();
		// 轮播图
		Carousel carousel = new Carousel();
		List<Carousel> caroueslList = (List<Carousel>) carouselService.listPage(pagination, carousel).get("list");
		List<PathDto> carouselPathDto = new ArrayList<PathDto>(caroueslList.size());
		for (Carousel record : caroueslList) {
			PathDto dto = new PathDto();
			dto.setName(record.getTitle());
			dto.setContent(record.getContent());
			dto.setPath(FileUtil.getUrl(filesService.selectById(record.getImgBg())));
			if (record.getImgLayer() != null) {
				dto.setPath2(FileUtil.getUrl(filesService.selectById(record.getImgLayer())));
			}
			dto.setPath3(record.getLayerLink());
			dto.setDesc(record.getFontColor());
			carouselPathDto.add(dto);
		}
		WebContext.setAttribute("carouselPathDto", carouselPathDto);

		pagination.setRows(4);
		// 新品推荐
		WebContext.setAttribute("recommendText", templateService.selectById(3));

		Product product = new Product();
		product.setIsTj(Const.STATUS_YES);
		Map<String, Object> result = productService.listPage(pagination, product);
		List<Product> productList = (List<Product>) result.get("list");
		List<PathDto> productPathDto = new ArrayList<PathDto>(productList.size());
		for (Product record : productList) {
			PathDto dto = new PathDto();
			dto.setId(record.getId());
			dto.setName(record.getName());
			dto.setContent(record.getContent());
			dto.setPath(FileUtil.getUrl(filesService.selectById(record.getImgLogo())));
			productPathDto.add(dto);
		}
		WebContext.setAttribute("productPathDto", productPathDto);
		WebContext.setAttribute(Const.PAGINATION, result.get("page"));
		return "/index";
	}

	@RequestMapping(value = "/getNewArrival")
	@ResponseBody
	public Object newArrival(Pagination pagination) {
		pagination.setRows(4);
		Product product = new Product();
		product.setIsTj(Const.STATUS_YES);
		Map<String, Object> result = productService.listPage(pagination, product);
		List<Product> productList = (List<Product>) result.get("list");
		List<PathDto> productPathDto = new ArrayList<PathDto>(productList.size());
		for (Product record : productList) {
			PathDto dto = new PathDto();
			dto.setId(record.getId());
			dto.setName(record.getName());
			dto.setContent(record.getContent());
			dto.setPath(FileUtil.getUrl(filesService.selectById(record.getImgLogo())));
			productPathDto.add(dto);
		}
		result.put("list", productPathDto);
		return result;
	}

	/**
	 * 关于我们
	 * 
	 * @return
	 */
	@RequestMapping(value = "/aboutUs")
	public String aboutUs() {
		HttpServletRequest request = WebContext.currentRequest();
		String html = templateService.selectById(2).getHtml().replaceAll("\\$\\{path\\}", request.getContextPath());
		html = html.replaceAll("\\$\\{imgpath\\}", ConfigUtil.get("website.domain"));
		request.setAttribute("aboutUsHtml", html);
		navigation();
		return "/aboutUs";
	}

	/**
	 * 导航信息查询
	 * 
	 * @param request
	 * @param carouselService
	 * @param productService
	 */
	public static void navigation() {
		HttpServletRequest request = WebContext.currentRequest();
		FilesService filesService = (FilesService) SpringUtils.getBean("filesService");
		BrandService brandService = (BrandService) SpringUtils.getBean("brandService");
		ProductCategoryService productCategoryService = (ProductCategoryService) SpringUtils.getBean("productCategoryService");
		if (request == null || filesService == null) {
			return;
		}
		// 品牌
		if (brandService != null) {
			Pagination p = new Pagination();
			p.setRows(8);
			List<Brand> list = (List<Brand>) brandService.listPage(p, new Brand()).get("list");
			List<PathDto> brandPathDto = new ArrayList<PathDto>(list.size());
			for (Brand b : list) {
				PathDto dto = new PathDto();
				dto.setId(b.getId());
				dto.setName(b.getName());
				dto.setPath(FileUtil.getUrl(filesService.selectById(b.getImgLogo())));
				brandPathDto.add(dto);
			}
			request.setAttribute("brandPathDto", brandPathDto);
		}
		// 产品类型
		if (productCategoryService != null) {
			Pagination p = new Pagination();
			p.setRows(8);
			ProductCategory where = new ProductCategory();
			where.setDisabled(Const.STATUS_ENABLED);
			List<ProductCategory> list = (List<ProductCategory>) productCategoryService.listPage(p, where).get("list");
			List<PathDto> productCategoryPathDto = new ArrayList<PathDto>(list.size());
			for (ProductCategory pc : list) {
				PathDto dto = new PathDto();
				dto.setId(pc.getId());
				dto.setName(pc.getName());
				dto.setPath(FileUtil.getUrl(filesService.selectById(pc.getImgLogo())));
				productCategoryPathDto.add(dto);
			}
			request.setAttribute("productCategoryPathDto", productCategoryPathDto);
		}

		// 加载foot
		foot();
	}

	public static void foot() {
		HttpServletRequest request = WebContext.currentRequest();
		TemplateService templateService = (TemplateService) SpringUtils.getBean("templateService");
		String html = templateService.selectById(1).getHtml().replaceAll("\\$\\{path\\}", request.getContextPath());
		request.setAttribute("footHtml", html);
	}
}
